<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户名称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户ID"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>  
      <el-form-item label="用户等级" prop="grade">
        <el-select
          v-model="queryParams.grade"
          placeholder="请选择用户等级"
          clearable
        >
          <el-option 
            v-for="dict in  dict.type.user_level_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.user_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table class="box1" v-loading="loading" :data="orderList">
      <el-table-column label="用户ID" align="center" prop="userId" width="200" />
      <el-table-column label="用户名称" align="center" prop="nickname" width="150" />
      <el-table-column label="手机号码" align="center" prop="mobile" width="150" />
      <el-table-column label="账户余额" align="center" prop="balance" width="150" />
      <el-table-column label="邀请码" align="center" prop="salt" />
      <el-table-column label="推荐人ID" align="center" prop="parentId" width="180"  />
      <el-table-column label="用户等级" align="center" prop="grade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_level_list" :value="scope.row.grade"/>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="note" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <div style="display: flex;justify-content:  center;flex-direction: column;">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.status == 2"
              @click="userDeal(scope.row,1)"
            >启用</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.status == 1"
              @click="userDeal(scope.row,2)"
            >禁用</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="userMoney(scope.row,2)"
            >操作余额</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="userLevel(scope.row)"
            >调整等级</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="openMoney" width="500px" append-to-body @close="closeFunc">
      <el-form ref="formMoney" :model="formMoney" :rules="rules" label-width="80px">
        <el-form-item label="操作类型" prop="add">
          <el-radio-group ref="radio" v-model="formMoney.add">
            <el-radio :label="true">充值</el-radio>
            <el-radio :label="false">划扣</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="金额" prop="balance">
          <el-input v-model="formMoney.balance" type="number" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="备注" prop="rema">
          <el-input
            v-model="formMoney.rema"
            type="textarea"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="调整用户等级" :visible.sync="openChangeLevel" width="400px" append-to-body @close="closeFunc">
      <el-form ref="formLevel" :model="formLevel" :rules="rules" label-width="80px">
        <el-form-item label="操作类型" prop="grade">
          <el-select v-model="formLevel.grade" placeholder="请选择用户等级" clearable>
            <el-option v-for="dict in dict.type.user_level_list" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitChangeLevel">确 定</el-button>
        <el-button @click="cancelChange">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { customerList, changeUser, changeBalance, changeLevel } from "@/api/customer/customer";
export default {
  name: "customerList",
  dicts: ["user_level_list","user_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "操作余额",
      // 是否显示弹出层
      open: false,
      openLevel: false,//调整等级探出窗
      openRelation: false,//调整关系链
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        userId: "",
        mobile: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        walletId: [{ required: true, message: "请选择账户类型", trigger: "change" }],
        income: [{ required: true, message: "请选择调整类型", trigger: "change" }],
        amount: [{ required: true, message: "请输入金额", trigger: "blur" }],
        memo: [{ required: true, message: "请输入备注", trigger: "blur" }],
        billLevel: [{ required: true, message: "请选择报单等级", trigger: "change" }],
        userLevel: [{ required: true, message: "请选择消费等级", trigger: "change" }],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        parentMobile: [{ required: true, message: "请输入上级手机号", trigger: "blur" }],

        add: [{ required: true, message: "请选择操作类型", trigger: "change" }],
        balance: [{ required: true, message: "请输入操作金额", trigger: "blur" }],
        rema: [{ required: true, message: "请输入操作备注", trigger: "blur" }],
        grade: [{ required: true, message: "请选择用户等级", trigger: "change" }],
      },
      openUp: false,
      openDown: false,
      upDataList: [],
      downDataList: [],
      totalUp: 0,
      totalDown: 0,
      userId: null,
      levelRemark:null,//调整等级备注
      userInfo: {},
      formMoney:{
        userId: null,
        add:null,
        balance:null,
        rema:null,
      },
      openMoney: false,
      openChangeLevel: false,
      formLevel:{
        id:null,
        grade: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 更改用户等级
    userLevel(data){
      this.$nextTick(()=>{
        this.$refs['formLevel'].clearValidate();
      })
      this.openChangeLevel = true
      this.formLevel = {
        id:data.id,
        grade: null,
      }
    },
    // 取消用户等级修改
    cancelChange(){
      this.openChangeLevel = false
      this.formLevel = {
        id:null,
        grade: null,
      }
    },
    // 提交修改用户等级
    submitChangeLevel(){
      this.$refs["formLevel"].validate((valid) => {
        if (valid) {
          changeLevel(this.formLevel).then(res => {
            if(res.code == 200){
              this.$modal.msgSuccess("操作成功");
              this.cancelChange()
              this.getList();
            }
          })
        }
      })
    },
    closeFunc(){
      this.openMoney = false
    },
    // 操作余额
    userMoney(data){
      this.userInfo = data
      this.formMoney = {
        userId: data.userId,
        add:null,
        balance:null,
        rema:null,
      }
      this.openMoney = true
    },
    // 提交
    submitForm(){
      this.$refs["formMoney"].validate((valid) => {
        if (valid) {
          changeBalance(this.formMoney).then(res => {
            if(res.code == 200){
              this.$modal.msgSuccess("操作成功");
              this.openMoney = false
              this.formMoney = {
                userId: null,
                add:null,
                balance:null,
                rema:null,
              }
            }
          })
        }
      })
    },
    cancel(){
      this.openMoney = false
      this.formMoney = {
        userId: null,
        add:null,
        balance:null,
        rema:null,
      }
    },
    // 用户操作
    userDeal(row, type){
      // 弹出窗确认
      this.$modal.confirm("确认执行操作吗？").then(() => {
        this.userChangeAjax(row, type);
      });
    },
    userChangeAjax(row, type){
      let data = {
        id: row.id,
        status: type,
      }
      changeUser(data).then((res) => {
        if(res.code == 200){
          this.$modal.msgSuccess("操作成功");
          this.getList();
        }
      });
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      if (this.dateRange.length > 0) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      customerList(this.queryParams).then((response) => {
        this.orderList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.beginTime = "";
      this.queryParams.endTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="css"> 
.el-tooltip__popper{ 
  max-width: 40%
}
</style>